<?php


namespace app\index\controller;

use think\App;
use think\Request;
use think\db;
use think\Controller;
use think\Loader;
use think\Log;
use mytool\Tree;

class Admin extends Controller
{
    public $uid;
    public function __construct(App $app = null)
    {
        $this->uid = session('uid');

        if(!$this->uid){
            $this->redirect('/index/Index/login');
        }
    }
    public function index(){
        return view("Admin/index");
    }
    // 验证码列表
    public function smslist(){
        return view("Admin/smslist");
    }
    public function postsmscode(){
    
       
        $where = array();
        $pageSize=input("get.limit/d");
        $page=input("get.page/d");
        if($page == 1){
           $page = 0; 
        }else{
            $page = ($page-1)*$pageSize;
        }
        $phone = input('get.phone');
        if($phone){
            $where['phone'] = $phone;
        }
      
        $count = db('verification_codes')->where($where)->count();    
        $list = db('verification_codes')->where($where)->order('id desc')->limit($page,$pageSize)->select();
        
        echo_info(0,'查询成功',$count,$list);
        
    }
    // 分析页
    public function workplace(){
        return view('Admin/workplace');
    }
    // 控制台
    public function console(){
        return view('Admin/console');
    }
    // 后台用户管理
    public function userSys(){
        return view("Admin/userSys");
    }
    // 文章管理
    public function  wenzhang(){
        return view("Admin/wenzhang");
    }
    // 添加文章
    public function block(){
        return view("Admin/block");
    }
    // 用户日志
    public function log(){
        return view("Admin/log");
    }
    public function getLog(){
        $list = db('login_logs')->select();
        echo_info(0,'查询成功',count($list),$list);
    }
    // 财务管理
    public function caiwu(){
        return view("Admin/caiwu");
    }
    public function getCaiwu(){
        $where['type'] = 2;
        $map=[];
        $pageSize=input("get.limit/d");
        $page=input("get.page/d");
        if($page == 1){
           $page = 0; 
        }else{
            $page = ($page-1)*$pageSize;
        }
        $phone = input('get.phone');
        if($phone){
            $where['phone'] = $phone;
        }
        $flag = input('get.flag');
        if($flag){
             $where['flag'] = $flag;
        }
        $count = db('jiesuan')->where($where)->count();    
        $list = db('jiesuan')->where($where)->order('id')->limit($page,$pageSize)->select();
        foreach ($list as $key => $val){
            $s = array(0=>'待处理',1=>"已打款",2=>"审核失败");
            $map['phone'] = $val['phone'];
            $users = db('users')->where($map)->find();
            $list[$key]['name'] = $users['name'];
            $yao = db('yao')->where($map)->find();
            $list[$key]['alipay'] = $yao['alipay_aocunt'];
            $list[$key]['s'] = $s[$val['flag']];
        }
        echo_info(0,'查询成功',$count,$list);
    }
    public function caiwuOk(){
         $where['id'] = input('post.id');
    
       
        $res = db('user_advertisement')->where($where)->delete();
        if($res){
            
            $this->success("操作成功");
        }else{
            $this->error("操作失败");
        } $this->error("审核失败");
        
    }
    public function caiwuFail(){
        $where['id'] = input('post.id');
    
       
        $res = db('user_advertisement')->where($where)->delete();
        if($res){
            
            $this->success("操作成功");
        }else{
            $this->error("操作失败");
        }
    }
    // 获取系统信息
    public function getConsole(){
        // 今日注册用户
        $start = date('Y-m-d')." 00:00:00";
        $end = date('Y-m-d')." 23:59:59";
        $data['todayRegUser'] = db('users')->where('createdAt', 'between time', [$start,$end])->count();
        // 今日群群数量
        $data['todayGroup'] = db('groups')->where('createdAt', 'between time', [$start,$end])->count();
        // 总用户
        $data['totalUser'] = db('users')->count();
        // 总群数量
        $data['totalGroup'] = db('groups')->count();
        // 今日提交认证用户
        $data['todayAuth'] = db('user_doc_list')->where('create_time', 'between time', [$start,$end])->where('data_type',1)->count();
        // 总提交认证用户
        $data['totalAuth'] = db('user_doc_list')->where('data_type',1)->count();
        $totalUser = db('users')->count();
        // 日登录用户，未登录用户
        $start = date('Y-m-d')." 00:00:00";
        $end = date('Y-m-d')." 23:59:59";

        $todayUser = db('users')->where('updatedAt', 'between time', [$start,$end])->count();
        $data['todayUser'] = $todayUser;
        $data['todayNotUser'] = $totalUser-$todayUser;
        // 周登录用户，未登录用户
        $start = date('Y-m-d',time()-7*24*60*60)." 00:00:00";
        $weekUser = db('users')->where('updatedAt', 'between time', [$start,$end])->count();
        $data['weekUser'] = $weekUser;
        $data['weekNotUser'] = $totalUser-$weekUser;
        // 月登录用户，未登录用户
        $start = date('Y-m-d',time()-30*24*60*60)." 00:00:00";
        $monthUser = db('users')->where('updatedAt', 'between time', [$start,$end])->count();
        $data['monthUser'] = $monthUser;
        $data['monthNotUser'] = $totalUser-$monthUser;
        echo_info(0,'查询成功',count($data),$data);
    }
    public function delActicle(){
        $where['id'] = input('post.id');
        $res = db('agreement_explain')->where($where)->delete();
        if($res){
            $this->success("成功");
        }else{
            $this->error("失败");
        }
    }
    // 添加文字Post
    public function addWenzhang(){
        $id = input('post.id');
        if($id){ // 更新
            $where['id'] = $id;
            $data['title'] = input('post.title');
            $data['content'] = input('post.demo');
            $data['explain_type'] = input('post.explain_type');
            $data['update_time'] = date('Y-m-d H:i:s');
            $res = db('agreement_explain')->where($where)->update($data);
            if($res){
                $this->success("成功");
            }else{
                $this->error("失败");
            }
        }else{
            // 新增
            $data['title'] = input('post.title');
            $data['content'] = input('post.demo');
            $data['explain_type'] = input('post.explain_type');
            $data['create_time'] = date('Y-m-d H:i:s');
            $res = db('agreement_explain')->insert($data);
            if($res){
                $this->success("成功");
            }else{
                $this->error("失败");
            }
        }
    }
    public function getArticle(){
        if(input('post.id')){
            $where['id'] = input('post.id');
            $list = db('agreement_explain')->where($where)->find();
            echo_info(0,'查询成功',count($list),$list);
        }
    }
    public function addActicle(){
        if(input('post.id') == 0){
            $data['title'] = input('post.title');
            $data['content'] = input('post.content');
            $data['explain_type'] = input('post.explain_type');
            $data['create_time'] = date('Y-m-d H:i:s');
            $res = db('agreement_explain')->insert($data);
            if($res){
                $this->success("添加成功");
            }else{
                $this->error("添加失败");
            }
        }else{
            $where['id'] = input('post.id');
            $data['title'] = input('post.title');
            $data['content'] = input('post.content');
            $data['explain_type'] = input('post.explain_type');
            $data['create_time'] = date('Y-m-d H:i:s');
            $res = db('agreement_explain')->where($where)->update($data);
            if($res){
                $this->success("修改成功");
            }else{
                $this->error("修改失败");
            }
        }
    }
    public  function getWenzhang(){
        $list = db('agreement_explain')->select();
        echo_info(0,'查询成功',count($list),$list);
    }
    public function config(){
        $data['data'] = db('config')->where('id',1)->find();
        $con = db('sys_config')->where('config_id=1')->find();
       
        $data['data']['config_val'] = $con['config_value'];
        return view("Admin/config",$data);
    }
    public function editSetting(){
        $where['id'] = input('post.id');
        $data['site'] = input('post.site');
        $data['miaoshu'] = input('post.miaoshu');
        $data['keys_word'] = input('post.keys_word');
        $data['sitename'] = input('post.sitename');
        $res = db('config')->where($where)->update($data);
        
            
        $config_val = input('post.config_val');
        $data2['config_value'] = $config_val;
        db('sys_config')->where('config_id=1')->update($data2);
        $this->success("修改成功");
       
    }
    // 后台用户数据请求
    public function userSysPost(){
        if(Request()->isGet()){
            $map=[];
            $pageSize=input("get.limit/d");
            $page=input("get.page/d");
            if($page == 1){
               $page = 0; 
            }else{
                $page = ($page-1)*$pageSize;
            }
            $username = input('get.username');
            $where = array();
            if($username){
                $where['username'] = $username;
            }
            $nickName = input('get.nickName');
            if($nickName){
                $where['nickName'] = $nickName;
            }
            $list = db('admin')->where($where)->order('id')->limit($page,$pageSize)->select();
            $count = db('admin')->where($where)->count(); 
            for($i=0; $i<count($list); $i++){
                $map['sj_phone'] = $list[$i]['phone'];
                $list[$i]['tj_count'] = db('yao')->where($map)->count();
                $yaolist = db('yao')->where($map)->field('phone')->select();
                $list[$i]['zr_count'] = 0;
                $str = "";
                foreach ($yaolist as $key => $val){
                    $str .= $val['phone'].",";
                }
                $str = substr($str,0,strlen($str)-1);
                
                $list[$i]['zr_count'] = db('Users')->where('phone','in',$str)->where('is_merchant',1)->count();
                
            }
            echo_info(0,'查询成功',$count,$list);
        }
    }
    // 编辑管理员
    public function editAdminuser(){
        $data = input("post.");
        $datas['phone'] = $data['phone'];
        $datas['nickName'] = $data['nickName'];
        $where['id'] = $data['id'];
        $pass = $data['password'];
        if($pass){
            $datas['password'] = md5($pass);
        }
        $res = db('admin')->where($where)->update($datas);
        if($res){
            $this->success("修改成功");
        }else{
            $this->error("修改失败");
        }
    }
    // 添加管理员
    public function addAdminuser(){
        $data = input("post.");
        if($_POST['password'] != $_POST['password2']){
            $this->error("密码不一致");
        }
        $datas['createTime'] = date('Y-m-d H:i:s');
        $datas['password'] = md5($data['password']);
        $datas['username'] = $data['username'];
        $datas['phone'] = $data['phone'];
        $datas['nickName'] = $data['nickName'];
        $datas['role'] = $data['role'];
        $where['username'] = $data['username'];
        $isF = db('admin')->where($where)->find();
        if($isF){
            $this->error("用户已存在");
        }
        $res = db('admin')->insert($datas);
        if($res){
            $this->success("添加成功");
        }else{
            $this->error("添加失败");
        }
    }
    // 修改状态
    public function changeUserState(){
        $data = input('post.');
        $where['id'] = $data['userId'];
        $datas['state'] = $data['state'];
        $res = db('admin')->where($where)->update($datas);
        if($res){
            $this->success("修改成功");
        }else{
            $this->error("修改失败");
        }
    }
    // 删除管理员
    public function delAdmin(){
        $where['id'] = input('post.id');
        $res = db('admin')->where($where)->delete();
        if($res){
            $this->success("成功");
        }else{
            $this->error("失败");
        }
    }
   
    // 退出登录
    public function loginout(){
        session('uid',null);
        $this->redirect('/index/Index/login');
    }
}